package net.qunzhi.modules.job.utils;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;

public class SampleTriggerListener implements TriggerListener {

    private static boolean previousJobCompleted = true;

    public static boolean previousJobCompleted() {
        return previousJobCompleted;
    }

    public static void setIsPreviousJobCompleted(boolean value) {
        previousJobCompleted = value;
    }

    @Override
    public String getName() {
        return "监听器";
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext jobExecutionContext) {
        System.out.println(" Trigger 被触发了，此时Job 上的 execute() 方法将要被执行");
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext ctx) {
        if(!previousJobCompleted) {
            System.out.println("发现此次Job的相关资源准备存在问题，不便展开任务，返回true表示否决此次任务执行");
            return true;
        }
        return false;
    }

    @Override
    public void triggerMisfired(Trigger trigger) {
        System.out.println( "当前Trigger触发错过了");
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext jobExecutionContext, Trigger.CompletedExecutionInstruction completedExecutionInstruction) {
        System.out.println("Trigger 被触发并且完成了 Job 的执行,此方法被调用");
        previousJobCompleted = true;
    }
}

